import Vue from 'vue';
import VueRouter from 'vue-router';
import login from '@/views/LogeView.vue';
import home from '@/views/HomeView.vue';
import store from '@/store'
import { loginStatus } from '@/api/index'
import { Message } from 'element-ui'
Vue.use(VueRouter)
const routes = [
  {
    path: '/',
    redirect: "/login"
  },
  {
    path: '/login',
    component: login
  },
  {
    path: '/home',
    component: home,
    redirect: '/admin',
    children: [
      {
        path: '/Admin',
        component: () => import('@/views/AdminView.vue'),
        meta: { title: '管理员首页', icon: 'el-icon-s-data' }
      },
      {
        path: '/Depar',
        component: () => import('@/views/DepartmentView.vue'),
        meta: { title: '系部管理', icon: 'el-icon-s-platform', id: 1 }

      },
      {
        path: '/Major',
        component: () => import('@/views/MajorView.vue'),
        meta: { title: '专业管理', icon: 'el-icon-s-flag', id: 2 }
      },
      {
        path: '/Class',
        component: () => import('@/views/ClassView.vue'),
        meta: { title: '班级管理', icon: 'el-icon-s-shop', id: 4 }
      },
      {
        path: '/Student',
        component: () => import('@/views/StudentView.vue'),
        meta: { title: '学生管理', icon: 'el-icon-s-marketing', id: 8 }
      },
      {
        path: '/Teacher',
        component: () => import('@/views/TeacherView.vue'),
        meta: { title: '教师管理', icon: 'el-icon-s-order', id: 16 }
      },
      {
        path: '/course',
        component: () => import('@/views/CourseView.vue'),
        meta: { title: '课程管理', icon: 'el-icon-menu', id: 32 }
      },
      {
        path: '/Select',
        component: () => import('@/views/SelectView.vue'),
        meta: { title: '选课修改', icon: 'el-icon-collection-tag', id: 64 }
      }, {
        path: '/Administrator',
        component: () => import('@/views/AdministratorView.vue'),
        meta: { title: '管理员管理', icon: 'el-icon-pie-chart', id: 128 }
      }
    ]
  },
  {
    path: '/teacher',
    component: home,
    redirect: "/teacher/admin",
    children: [
      {
        path: '/teacher/admin',
        component: () => import("@/views/AdminView.vue"),
        meta: {
          title: "首页"
        }
      },
      {
        path: '/lecture',
        component: () => import("@/views/teacher/LectureView.vue"),
        meta: {
          title: "授课查询"
        }
      }
      ,
      {
        path: '/schedule',
        component: () => import("@/views/teacher/ScheduleView.vue"),
        meta: {
          title: "教课课表"
        }
      }
      ,
      {
        path: '/mark',
        component: () => import("@/views/teacher/LectureView.vue"),
        meta: {
          title: "成绩录入"
        }
      }
    ]

  }
]
let originalPush = VueRouter.prototype.push // 先保存一份VueRouter.prototype.push方法
let originalReplace = VueRouter.prototype.replace

VueRouter.prototype.push = function push(location) {
  // call()：篡改上下文    catch()：捕获异常
  return originalPush.call(this, location).catch(e => e)
}

VueRouter.prototype.replace = function replace(location) {
  // call()：篡改上下文    catch()：捕获异常
  return originalReplace.call(this, location).catch(e => e)
}
const router = new VueRouter({
  // mode: 'history',
  // base: process.env.BASE_URL,
  routes
})
// 路由守卫（BA）
router.beforeEach(async (to, from, next) => {
  // to 跳转到哪
  // from 从哪来
  // next 通过
  // console.log(to);
  // loginStatus().then(res => {
  //   console.log(res);

  // })
  // next()
  // 三个功能，判断是否登录
  // 1，使用接口验证是否登录
  // 2，判断是否有权限

  // 如果登录了，就正常通过
  // 如果不登陆，就回到登录页

  if (to.path == "/login") {
    //   // 如果是登录页面就通过
    next()
  } else {
    if (store.state.xixi.userInfo.loggedIn) {
      // 路由权限
      // 8 路由的权限
      // [1,2,4] 用户的权限组 
      // console.log(to.meta.id, 123);
      // console.log(store.state.user.userInfo, 123);

      // !to.meta.id 没有权限
      // store.state.user.userInfo.permissions.indexOf(to.meta.id) != -1  用户权限包含 路由权限
      if (!to.meta.id || store.state.xixi.userInfo.permissions.indexOf(to.meta.id) != -1) {
        next()
      } else {
        // 没有权限的情况下
        Message({
          message: "该用户没有该权限",
          type: 'error'
        });
        next("/admin", () => { })
      }



    } else {
      // 登录验证接口
      loginStatus().then(res => {
        console.log(res, "登录验证接口");
        if (res.data.loggedIn) {
          store.commit("xixi/changeUserInfo", res.data)
          let group = [];
          for (let bit = 0; bit < 8; bit++) {
            // 0100 0000
            // 0010 0000
            if (((1 << bit) & res.data.permission) === 1 << bit) {
              group.push((1 << bit));
            }
          }
          // console.log(group);
          res.data["permissions"] = group
          next(to.path, () => { })
        } else {
          next("/login")
        }
      })
    }
  }

})
export default router
